package com.haoyeteng.sxbd.modular.business.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.haoyeteng.sxbd.core.base.controller.BaseController;
import com.haoyeteng.sxbd.core.common.constant.factory.PageFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.haoyeteng.sxbd.core.base.model.ResponseResult;
import com.haoyeteng.sxbd.core.common.annotion.Permission;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.haoyeteng.sxbd.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.haoyeteng.sxbd.modular.business.model.Product;
import com.haoyeteng.sxbd.modular.business.service.IProductService;

/**
 * 服务商品表控制器
 *
 * @author haoyeteng
 * @Date 2019-06-20 21:26:19
 */
@Controller
@RequestMapping("/product")
public class ProductController extends BaseController {

    private String PREFIX = "/business/product/";

    @Autowired
    private IProductService productService;

    /**
     * 跳转到服务商品表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "product.html";
    }

    /**
     * 跳转到添加服务商品表
     */
//    @RequestMapping("/product_add")
//    public String productAdd() {
//        return PREFIX + "product_add.html";
//    }

    /**
     * 跳转到修改服务商品表
     */
    @RequestMapping("/product_update/{productId}")
    public String productUpdate(@PathVariable Integer productId, Model model) {
        Product product = productService.selectById(productId);
        model.addAttribute("item",product);
        LogObjectHolder.me().set(product);
        return PREFIX + "product_edit.html";
    }

    /**
     * 获取服务商品表列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission
    public ResponseResult<Object> list(String productName) {
        Page<Product> page = new PageFactory<Product>().defaultPage();
        List<Map<String,Object>>  products = productService.selectProducts(page, productName);
    	ResponseResult<Object> result = new ResponseResult<Object>();
        result.setData(products);
 		result.setCount(page.getTotal());
     	result.setCode(SUCCESS_TIP.getCode());
     	result.setMsg(SUCCESS_TIP.getMessage());
 		return result;
    }

    /**
     * 新增服务商品表
     */
//    @RequestMapping(value = "/add")
//    @ResponseBody
//    @Permission
//    public Object add(Product product) {
//        productService.insert(product);
//        return SUCCESS_TIP;
//    }

    /**
     * 删除服务商品表
     */
//    @RequestMapping(value = "/delete")
//    @ResponseBody
//    @Permission
//    public Object delete(@RequestParam Integer productId) {
//        productService.deleteById(productId);
//        return SUCCESS_TIP;
//    }

    /**
     * 修改服务商品表
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission
    public Object update(Product product) {
        product.setUpdateTime(new Date());
        productService.updateById(product);
        return SUCCESS_TIP;
    }

    /**
     * 服务商品表详情
     */
//    @RequestMapping(value = "/detail/{productId}")
//    @ResponseBody
//    public Object detail(@PathVariable("productId") Integer productId) {
//        return productService.selectById(productId);
//    }
}
